(function($, doc) {
	$.init();
	var userinfo = app.getState() || {};
	var uid=userinfo.user_id; 
	
	var address= md5(uid) ;
	address=address.toUpperCase();
	doc.getElementById('address').innerHTML = address;
		
/*	if(userinfo.headimgurl && userinfo.headimgurl.indexOf('Uploads') !== -1) {
	    if(userinfo.headimgurl.indexOf(upurl+'Uploads') !== -1){
			doc.getElementById('headimg').src=userinfo.headimgurl;
		}
		else{
			doc.getElementById('headimg').src=upurl+userinfo.headimgurl;
		}
	} else {
		doc.getElementById('headimg').src = 'img/logo.png';
	}
	*/	
	var click = {
		data:{
			i:0
		},
		init:function(){
			var size = click.data.i;
			var exchange = doc.getElementById('exchange_rate').innerText;
			var oChongzhiMenu = document.querySelector('.chongzhi_menu').querySelectorAll('a');
			var kap_price = document.getElementById('kap_price').innerText;
			var tg_count = document.getElementById('tg_count');
			var kap_count = document.getElementById('kap_count');
			
			for(var i in oChongzhiMenu){
				oChongzhiMenu[i].index = i;
				oChongzhiMenu[i].onclick=function(e){
					oChongzhiMenu[size].className=''
					
					size=this.index;
					this.className='active';
					tg_count.innerText = e.target.id;
					
					var kcount = tg_count.innerText /( exchange * kap_price)
					kap_count.innerText = kcount.toFixed(4);
				}
			}
		}
	}

	$.plusReady(function() {
		
		mui.ajax({
			url: apiurl + "User/userInfo/",
			data: {
				'user_id': uid,
				'_token':userinfo.token
			},
			async: true,
			dataType: 'json',
			crossDomain: true, //强制使用5+跨域
			type: 'get',
			timeout: 10000,
			success: function(res) {
				console.log(JSON.stringify(res));
				// 请求成功
				if(res.status ==-3){
					mui.toast(res.msg);
					app.out();
					return false;
				} else {
					userinfo.a1_money = res.data.a1_money;
					userinfo.a2_money = res.data.a2_money;
					userinfo.a3_money = res.data.a3_money;
					app.setState(userinfo);
					
					var balance3 = parseFloat(res.data.a3_money);
					var balance1 = parseFloat(res.data.total_kap - res.data.get_kap);
					var pay_money = parseFloat(res.data.pay_money);
					doc.getElementById('balance3').innerHTML = balance3.toFixed(2);	//可交易资产
					doc.getElementById('balance1').innerHTML = balance1.toFixed(2); //托管中
					doc.getElementById('pay_money').innerHTML = pay_money.toFixed(2); //托管总量
					doc.getElementById('kap_price').innerHTML = res.data.kap_price;
					doc.getElementById('exchange_rate').innerHTML = res.data.exchange_rate;
					
					var add = template('list-template', {
						"tradelist": res.data.get_money
					});
					
					doc.getElementById("mytradelist").innerHTML = add;
					click.init();
				}
			},
			error: function(xhr, type, errorThrown) {
				mui.toast('操作失败！');
			}
		});
		
		doc.getElementById('tijiao').addEventListener('tap', function(){
			var data = {
				money: document.getElementById('tg_count').innerText,
				trade_pwd: document.getElementById('password_account').value,
				_token:app.token(18)
			};
			
			if(data.account=='') {
				mui.toast('请选择托管数量！');
				return;
			}
			
			if(data.trade_pwd.length < 6) {
				mui.toast('交易密码最短为 6 个字符！');
				return;
			}

			console.log(apiurl + "Trade/applykap/");


			mui.ajax({
				url: apiurl + "Trade/applykap/",
				data: data,
				async: true,
				dataType: 'json',
				crossDomain: true, //强制使用5+跨域
				type: 'post',
				timeout: 10000,
				success: function(res) {
					console.log(JSON.stringify(res));
					mui.toast(res.msg);
					if(res.status == 1){
						location.reload();
					}
				},
				error: function(xhr, type, errorThrown) {
					mui.toast('操作失败！');
				}
			});
			
		}, false);
		
		
/*		doc.getElementById('tuoguan_order').addEventListener('tap', function(){
			 //打开找回密码页
			mui.openWindow({
			    id:'chainlist.html',
			    url:'/pages/wallet/chainlist.html',
			    show: {
					aniShow: 'pop-in',
					autoShow:false
				},
				waiting: {
					autoShow:true,//自动显示等待框，默认为true
                    title:'正在加载...',//等待对话框上显示的提示内容
				}
			});
		}, false);
*/		
		document.getElementById('tuoguan_order').addEventListener('tap', function() {
		//打开找回密码页
		mui.openWindow({
			id: 'account_detail.html',
			url: '/pages/wallet/account_detail.html',
			show: {
				aniShow: 'pop-in',
				autoShow: false
			},
			waiting: {
				autoShow: true, //自动显示等待框，默认为true
				title: '正在加载...', //等待对话框上显示的提示内容
			}
		});
	}, false);
		
		doc.getElementById('zhinan').addEventListener('tap', function(){
			 //打开找回密码页
			mui.openWindow({
			    id:'zhinan.html',
			    url:'/pages/wallet/zhinan.html',
			});
		}, false);
	})
		
	
}(mui, document));